import java.util.Enumeration;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

public class FileThread extends Thread {

	String fileName=null;
    FileThreadInvoker invoker=null;
    
    /** Creates a new instance of PlayerThread */
    public FileThread(FileThreadInvoker invoker, String filename) {
        this.invoker=invoker;
        this.fileName=filename;        
    }
    
    // The run method creates a player and starts the playback of the audiofile
    public void run(){
    	deleteFile(fileName);       
        invoker.fileDeleted();
    }
    
    private void deleteContentDir(FileConnection fc, String ruta){
    	// Elimina todos los ficheros de un directorio
    	try{
    		Enumeration lista = fc.list(Constantes.EXTENSION_TODOS, true);
    		if (lista == null){
    			return;
    		}
    		// Add all content to the dirList vector
    		while (lista.hasMoreElements()) {
    			String s = (String) lista.nextElement();
    			deleteFile(ruta + s);
    		} 
    	}catch (Exception e) {

		}
    	
    }
    
    private void deleteFile(String fichero){
    	FileConnection fc = null;
        try{
        	fc = (FileConnection) Connector.open(fichero, Connector.READ_WRITE);
        	if (fc.exists()) {
        		if (fc.isDirectory()){
        			deleteContentDir(fc, fichero);
        		}        		
        		fc.delete();        		
        	}        	
        }catch(Exception e){
        	//e.printStackTrace();
        }finally{
        	try{
        		fc.close();		
        	}catch (Exception e) {
			}
        }
    }
    
    // close the player and end the playback
    public void close(){
        try{
            
        }catch(Exception e){
        	//e.printStackTrace();
        }
    }    
}
